//
//  SegmentedCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/24/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit

class SegmentedCell: UITableViewCell, ConfigurableCell, OptionCellProtocol {
    
    var delegate: OptionCellActionProtocol!
    var segmentView: RoundedSCView!
    var data: Segmented!
    
    @objc func valueChange (sender: Any) {
        delegate.changeCarType(newValue: segmentView.currentValue, item: data)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        segmentView = RoundedSCView()
        
        self.contentView.backgroundColor = UIColor(red: 244/255, green: 246/255, blue: 250/255, alpha: 1)
        self.contentView.addSubview(segmentView)
        
        segmentView.snp.makeConstraints { (make) in
            make.height.equalTo(40)
            make.centerY.equalToSuperview()
            make.leading.trailing.equalToSuperview().inset(16)
            make.bottom.equalToSuperview().inset(8)
            make.top.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configure(data object: Segmented, index: Int) {
        if (self.data == nil || self.data.segments != object.segments) {
            self.data = object
            segmentView.updateItems(items: object.segments)
        } 
        segmentView.transportSelect.addTarget(self, action: #selector(valueChange(sender:)), for: .valueChanged)
    }
    
    func returnData() -> Segmented {
        let segmented = Segmented(segments: [])
        return segmented
    }
}
